package com.czn.service.impl;

import com.czn.dao.OptionDao;
import com.czn.entity.Option;
import com.czn.service.IOptionService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ChenZeNan
 * @create 2021-04-22 22:13
 */
@Service
public class OptionServiceImpl implements IOptionService {

    @Autowired
    private OptionDao optionDao;

    /**
     * 分页/模糊查询科室信息
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @Override
    public PageQuery<Option> findOptionList(Integer pageNo, Integer pageSize, String name, String type) {
        LambdaQuery<Option> query = optionDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            query.andLike(Option::getName, "%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            query.andLike(Option::getType, "%" + type + "%");
        }
        if (pageNo > 0 && pageSize > 0) {
            PageQuery<Option> page = query.desc(Option::getId).page(pageNo, pageSize);
            return page;
        }
        return null;
    }

    /**
     * @param option
     * @return
     */
    @Override
    public ResponseResult addOption(Option option) {
        ResponseResult result = new ResponseResult();
        // 根据身份证查询数据库中是否已有该对象了
        LambdaQuery<Option> query = optionDao.createLambdaQuery();
        query.andEq(Option::getName, option.getName());
        Option sysOption = query.single();
        if (sysOption == null) { // 数据库中没有该对象，直接插入
            optionDao.insert(option);
            result.setCode("300");
            result.setMessage("信息添加成功！");
            return result;
        }
        // 数据库中已有该对象了
        result.setCode("301");
        result.setMessage("该科目信息已存在数据库中~");
        return result;
    }

    /**
     * 编辑科室
     *
     * @param option
     * @return
     */
    @Override
    public ResponseResult editOption(Option option) {
        ResponseResult result = new ResponseResult();
        int rows = optionDao.updateById(option);
        if (rows > 0) {
            result.setCode("500");
            result.setMessage("信息更新成功！");
            return result;
        }
        result.setCode("501");
        result.setMessage("信息更新失败！");
        return result;
    }

    /**
     * 根据id查询科室
     *
     * @param id
     * @return
     */
    @Override
    public Option findOptionById(Integer id) {
        Option option = optionDao.single(id);
        return option;
    }

    /**
     * 根据id删除科室
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteOptionById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = optionDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }

    /**
     * 查找所有检查记录
     *
     * @return
     */
    @Override
    public List<Option> findAll() {
        List<Option> all = optionDao.all();
        return all;
    }
}
